//
//  XGGuestViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/19.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <SVProgressHUD/SVProgressHUD.h>

#import "XGGuestViewController.h"
#import "XGUserListTableViewController.h"

#import "XGCollectionHeaderView.h"

/// headerView重用标识符
static NSString *const XGCollectionHeaderViewReuseIdentifier = @"XGCollectionHeaderView";

@interface XGGuestViewController () <XGCollectionHeaderViewDelegate>

@end

@implementation XGGuestViewController

#pragma mark - 其他方法

/// 设置collectionView
- (void)setUpCollectionView
{
    [super setUpCollectionView];
    
    // 布局
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    // 计算header头部
    CGFloat height = 20 + 80 + 10;
    height += [[XGAccountViewModel currentUser].screenName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:20]}].height + 10;
    height += [self.user[@"descriptionInfo"] boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size.height + 5;
    height = ceil(height);
    flowLayout.headerReferenceSize = CGSizeMake(self.view.width, height);
    
    // 注册headerView
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([XGCollectionHeaderView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XGCollectionHeaderViewReuseIdentifier];
}

#pragma mark - UICollectionViewDelegate

// 返回headerView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        XGCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:XGCollectionHeaderViewReuseIdentifier forIndexPath:indexPath];
        // 设置用户数据
        headerView.user = self.user;
        // 设置代理
        headerView.delegate = self;
        // 设置按钮标题
        [headerView.editOrFocusButton setTitle:@"关注" forState:UIControlStateNormal];
        [headerView.editOrFocusButton setTitle:@"√ 已关注" forState:UIControlStateSelected];
        
        // 设置按钮状态
        AVQuery *query = [[AVQuery alloc] initWithClassName:@"_Followee"];
        [query whereKey:@"user" equalTo:[XGAccountViewModel currentUser]];
        [query whereKey:@"followee" equalTo:self.user];
        [query countObjectsInBackgroundWithBlock:^(NSInteger number, NSError * _Nullable error) {
            headerView.editOrFocusButton.selected = number != 0;
            headerView.editOrFocusButton.backgroundColor = number != 0 ? [UIColor lightGrayColor] : [UIColor redColor];
        }];
        return headerView;
    }
    
    return nil;
}

#pragma mark - XGCollectionHeaderViewDelegate

/// 点击headerView帖子数代理方法
- (void)collectionHeaderViewTopicCountDidClick:(XGCollectionHeaderView *)headerView
{
    if (self.topicList.count > 0) {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
    }
}

/// 点击headerView粉丝数代理方法
- (void)collectionHeaderViewFansCountDidClick:(XGCollectionHeaderView *)headerView
{
    XGUserListTableViewController *viewController = [XGUserListTableViewController viewControllerWithUserInfo:self.user listType:XGUserListTypeFollower];
    [self.navigationController pushViewController:viewController animated:YES];
}

/// 点击headerView关注数代理方法
- (void)collectionHeaderViewFocusCountDidClick:(XGCollectionHeaderView *)headerView
{
    XGUserListTableViewController *viewController = [XGUserListTableViewController viewControllerWithUserInfo:self.user listType:XGUserListTypeFollowee];
    [self.navigationController pushViewController:viewController animated:YES];
}

/// 点击headerView按钮代理方法
- (void)collectionHeaderViewEditOrFocusDidClick:(XGCollectionHeaderView *)headerView button:(UIButton *)button
{
    button.selected = !button.isSelected;
    
    if (button.isSelected) {
        // 关注
        button.backgroundColor = [UIColor lightGrayColor];
        [[XGAccountViewModel currentUser] follow:self.user.objectId andCallback:^(BOOL succeeded, NSError * _Nullable error) {
            if (error != nil || !succeeded) {
                [SVProgressHUD showErrorWithStatus:@"关注失败,请检查网络连接"];
                return;
            }
            
            [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        }];
    } else {
        // 取消关注
        button.backgroundColor = [UIColor redColor];
        [[XGAccountViewModel currentUser] unfollow:self.user.objectId andCallback:^(BOOL succeeded, NSError * _Nullable error) {
            if (error != nil || !succeeded) {
                [SVProgressHUD showErrorWithStatus:@"取消关注失败,请检查网络连接"];
                return;
            }
            
            [SVProgressHUD showSuccessWithStatus:@"取消关注成功"];
        }];
    }
}

@end
